package com.ybwh.springboot2.satoken.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.ybwh.springboot2.common.RespData;
import com.ybwh.springboot2.satoken.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author saytime
 * @since 2018-12-06
 */
@Slf4j
@Api(tags = "登录接口")
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;


    @ApiOperation(value = "登录", notes = "")
    @ResponseBody
    @PostMapping(value = "/login")
    public RespData<String> login(@RequestParam("username") String username, @RequestParam("password") String password) {


        String token = sysUserService.login(username, password);


        return RespData.successResp(token);
    }


    @ApiOperation(value = "退出", notes = "")
    @ResponseBody
    @PostMapping(value = "/logout")
    public RespData<Object> logout() {
        log.info("----------------isLogin={}", StpUtil.isLogin());

        if (!StpUtil.isLogin()) {
            return RespData.successResp(null);
        }

        String token = StpUtil.getTokenValue();
        // 当前会话注销登录
        StpUtil.logoutByTokenValue(token);

        return RespData.successResp(null);
    }

}
